package com.grandprixsw.evmsmobile;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
	private int rootId;
	
	public class FormulaClickListener implements OnClickListener {
    	public void onClick(View v) {
    		showDialog(v.getId());
    	}
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootId = R.layout.main;
        setContentView(rootId);
        
        View costVarianceView = findViewById(R.id.cost_variance_view);
        costVarianceView.setOnClickListener(new FormulaClickListener());
        
        View scheduleVarianceView = findViewById(R.id.schedule_variance_view);
        scheduleVarianceView.setOnClickListener(new FormulaClickListener());
        
        View varianceAtCompletionView = findViewById(R.id.variance_at_completion_view);
        varianceAtCompletionView.setOnClickListener(new FormulaClickListener());
        
        View costEfficiencyView = findViewById(R.id.cost_efficiency_view);
        costEfficiencyView.setOnClickListener(new FormulaClickListener());
        
        View scheduleEfficiencyView = findViewById(R.id.schedule_efficiency_view);
        scheduleEfficiencyView.setOnClickListener(new FormulaClickListener());
        
        View percentCompleteView = findViewById(R.id.percent_complete_view);
        percentCompleteView.setOnClickListener(new FormulaClickListener());

        View percentSpentView = findViewById(R.id.percent_spent_view);
        percentSpentView.setOnClickListener(new FormulaClickListener());

        View tcpiView = findViewById(R.id.tcpi_view);
        tcpiView.setOnClickListener(new FormulaClickListener());
        
        View eacCpiView = findViewById(R.id.eac_cpi_view);
        eacCpiView.setOnClickListener(new FormulaClickListener());
        
        View eacCompositeView = findViewById(R.id.eac_composite_view);
        eacCompositeView.setOnClickListener(new FormulaClickListener());
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	
    	return GlobalOptionsMenu.CreateMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	return GlobalOptionsMenu.MenuChoice(this, item);
    }
    
    @Override
    public Dialog onCreateDialog(int id) {
    	Dialog dlg;
    	
		switch(id) {
		case R.id.cost_variance_view:
			dlg = createSimpleDialog(R.string.cost_variance, R.layout.formula_cost_variance_details_view);
			break;
		case R.id.schedule_variance_view:
			dlg = createSimpleDialog(R.string.schedule_variance, R.layout.formula_schedule_variance_details_view);
			break;
		case R.id.variance_at_completion_view:
			dlg = createSimpleDialog(R.string.variance_at_completion, R.layout.formula_variance_at_completion_details_view);
			break;
		case R.id.cost_efficiency_view:
			dlg = createSimpleDialog(R.string.cost_efficiency, R.layout.formula_cost_efficiency_details_view);
			break;
		case R.id.schedule_efficiency_view:
			dlg = createSimpleDialog(R.string.schedule_efficiency, R.layout.formula_schedule_efficiency_details_view);
			break;
		case R.id.percent_complete_view:
			dlg = createSimpleDialog(R.string.percent_complete, R.layout.formula_percent_complete_details_view);
			break;
		case R.id.percent_spent_view:
			dlg = createSimpleDialog(R.string.percent_spent, R.layout.formula_percent_spent_details_view);
			break;
		case R.id.tcpi_view:
			dlg = createSimpleDialog(R.string.tcpi, R.layout.formula_tcpi_details_view);
			break;
		case R.id.eac_cpi_view:
			dlg = createSimpleDialog(R.string.eac_cpi, R.layout.formula_eac_details_view);
			break;
		case R.id.eac_composite_view:
			dlg = createSimpleDialog(R.string.eac_composite, R.layout.formula_eac_composite_details_view);
			break;
		default:
			LayoutInflater inflater = getLayoutInflater();  
			View view = inflater.inflate(R.layout.one_line_formula_view, 
										   (ViewGroup) findViewById(rootId), 
										   false);

			TextView textView = (TextView) view.findViewById(R.id.one_line_formula_text);
			textView.setText(getResources().getString(R.string.missing_dialog_err));
			dlg = createSimpleDialog(R.string.error, textView);
		}
		
		return dlg;
    }
    
    private Dialog createSimpleDialog(int titleId, int viewId) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
		
		dialogBuilder.setTitle(getResources().getString(titleId));
		dialogBuilder.setNegativeButton(R.string.close_btn, 
				new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {}
		});

		LayoutInflater inflater = getLayoutInflater();  
		View view = inflater.inflate(viewId, (ViewGroup) findViewById(rootId), false);

		dialogBuilder.setView(view);
		
		return dialogBuilder.create();
    }
    
    private Dialog createSimpleDialog(int titleId, View view) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
		
		dialogBuilder.setTitle(getResources().getString(titleId));
		dialogBuilder.setNegativeButton(R.string.close_btn, 
				new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {}
		});

		dialogBuilder.setView(view);
		
		return dialogBuilder.create();
    }
        
}